<?php
/**
 * yuzubang_1模块微站定义
 *
 * @author rdmtlm
 * @url 
 */
defined('IN_IA') or exit('Access Denied');

class Yuzubang_1ModuleSite extends WeModuleSite {

	/** 房源信息表*/
	private $tb_house = 'yuzubang_1_house';

	
	/**
	 * 获取房源状态的文字描述
	 * @param int $status
	 * @return string
	 */
	private function getHouseStatus($status){
		$status = intval($status);
		if ($status == 0) {
			return '下架';
		} elseif ($status == 1) {
			return '上架';
		} else {
			return '未知';
		}
	}
	
	
	public function doWebHouse() {
		//这个操作被定义用来呈现 管理中心导航菜单
		global $_W, $_GPC;
		
		$ops = array('display', 'edit', 'delete'); // 只支持此 3 种操作.
		$op = in_array($_GPC['op'], $ops) ? $_GPC['op'] : 'display';
		
		if($op == 'display'){
			// 处理 GET 提交
			$pageindex = max(intval($_GPC['page']), 1); // 当前页码
			$pagesize = 10; // 设置分页大小
			
			$where = ' WHERE uniacid=:uniacid';
			$params = array(
				':uniacid'=>$_W['uniacid']
			);
			
			$sql = 'SELECT COUNT(*) FROM '.tablename($this->tb_house).$where;
			$total = pdo_fetchcolumn($sql, $params);
			$pager = pagination($total, $pageindex, $pagesize);
			
			$sql = 'SELECT * FROM '.tablename($this->tb_house)." {$where} ORDER BY id desc LIMIT ".(($pageindex -1) * $pagesize).','. $pagesize;
			$goodses = pdo_fetchall($sql, $params, 'id');
			
			load()->func('tpl');
			include $this->template('house_display');
		}
		
		if ($op == 'edit') {
			
			$id = intval($_GPC['id']);
			if(!empty($id)){
				$sql = 'SELECT * FROM '.tablename($this->tb_house).' WHERE id=:id AND uniacid=:uniacid LIMIT 1';
				$params = array(':id'=>$id, ':uniacid'=>$_W['uniacid']);
				$goods = pdo_fetch($sql, $params);
				
				if(empty($goods)){
					message('未找到指定的房源.', $this->createWebUrl('house'));
				}
			}
			
			
			if (checksubmit()) {
				$data = $_GPC['goods']; // 获取打包值
				
				if(empty($goods)){
					$data['uniacid'] = $_W['uniacid'];
					$data['create_time'] = TIMESTAMP;
					
					$ret = pdo_insert($this->tb_house, $data);
					if (!empty($ret)) {
						$id = pdo_insertid();
					}
				} else {
					$ret = pdo_update($this->tb_house, $data, array('id'=>$id));
				}
				
				if (!empty($ret)) {
					message('房源信息保存成功', $this->createWebUrl('house', array('op'=>'edit', 'id'=>$id)), 'success');
				} else {
					message('房源信息保存失败');
				}
			}
			
			load()->func('tpl');
			include $this->template('house_edit');
		}
		
		if($op == 'delete') {
			$id = intval($_GPC['id']);
			if(empty($id)){
				message('未找到指定房源');
			}
			$result = pdo_delete($this->tb_house, array('id'=>$id, 'uniacid'=>$_W['uniacid']));
			if(intval($result) == 1){
				message('删除房源成功.', $this->createWebUrl('house'), 'success');
			} else {
				message('删除房源失败.');
			}
		}
	}
	
	
	/**
	 * 前端 - 首页
	 */
	public function doMobileIndex() {
		global $_W, $_GPC;
		
		$where = ' WHERE uniacid=:uniacid and sale_status=1 and del=0';
		$params = array(
			':uniacid'=>$_W['uniacid']
		);
		$sql = 'SELECT * FROM '.tablename($this->tb_house)." {$where} ORDER BY id desc LIMIT 0, 10";
		$data = pdo_fetchall($sql, $params, 'id');
		
        foreach($data as $k=>$v){
            $v['imgs'] = explode(',', $v['imgs']);
            $data[$k] = $v; 
        }   
		
		include $this->template('index');
	}
	
	/**
	 * 房源页
	 */
	public function doMobileHouse() {
		global $_W, $_GPC;
		
		$ops = array('display', 'get_list', 'detail');
		$op = in_array($_GPC['op'], $ops) ? $_GPC['op'] : 'display';
		
		if($op == 'display'){
			
			include $this->template('house');
			
		}else if($op == 'get_list'){
			$pageindex = max(intval($_GPC['p']), 1); // 当前页码
			$pagesize = 10; // 设置分页大小
			
			$search = $_GPC['search'];
			$rental1 = $_GPC['rental1'];
			$rental2 = $_GPC['rental2'];
			$rent_type = $_GPC['rent_type'];
			
			$where = ' WHERE uniacid=:uniacid and sale_status=1 and del=0';
			$params = array(
				':uniacid'=>$_W['uniacid']
			);
			
			if($search !== ''){
				$where .= " and village_name like '%:search%'";
				$params[':search'] = $search;
			}
			if($rental1 !== ''){
				$rental1 = intval($rental1);
				$rental1 < 0 && $rental1 = '';
			}
			if($rental2 !== ''){
				$rental2 = intval($rental2);
				$rental2 < 0 && $rental2 = '';
			}
			if($rental1 !== '' && $rental2 !== ''){
				$where .= " and rental>:rental1 and rental<=:rental2";
				$params[':rental1'] = $rental1;
				$params[':rental2'] = $rental2;
			}else{
				if($rental1 !== ''){
					$where .= " and rental>:rental1";
					$params[':rental1'] = $rental1;
				}
				if($rental2 !== ''){
					$where .= " and rental<=:rental2";
					$params[':rental2'] = $rental2;
				}
			}
			if($rent_type !== ''){
				$where .= " and rent_type=:rent_type";
				$params[':rent_type'] = $rent_type;
			}
			
			
			$sql = 'SELECT * FROM '.tablename($this->tb_house)." {$where} ORDER BY update_time desc LIMIT ".(($pageindex -1) * $pagesize).','. $pagesize;
			$data = pdo_fetchall($sql, $params, 'id');
			
	        foreach($data as $k=>$v){
	            $v['imgs'] = explode(',', $v['imgs']);
	            $data[$k] = $v; 
	        }
	        
			ob_clean();
			ob_start();
			include $this->template('house_list');
			$content = ob_get_contents();
			ob_clean();	
	        
	        $result = array(
	        	'data'=>$content,
	        	'status'=>1,
	        	'count'=>count($data)
	        );
	        
	        exit(json_encode($result));
	        
		}else if($op == 'detail'){
			$hid = intval($_GPC['hid']);
			
			$sql = 'SELECT * FROM '.tablename($this->tb_house)." WHERE uniacid=:uniacid and id=:id";
			$params = array(
				':uniacid'=>$_W['uniacid'],
				':id'=>$hid
			);
			
			$data = pdo_fetch($sql, $params);
				
			if(empty($data)){
				message('未找到指定的房源.', $this->createMobileUrl('house'));
			}
			
			$data['imgs'] = explode(',', $data['imgs']);
			
			include $this->template('house_detail');
		}
		
	}
	

}